﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class MH_MuaHangNuocNgoai
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Dim DoanhThu, PhanTramThue, TienThue, TongCong As Decimal
    Dim MaHang, TenHang, DVT, LoHang As String
    Dim TaiKhoanNo, TaiKhoanCo, TaiKhoanThue As String
    Dim HSD As Date
    Private Sub setReadonly(ByVal b As Boolean)
        Me.SochungtuTextEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.NgaychungtuDateEdit.Properties.ReadOnly = b
        Me.MaDoiTuongSearchLookUpEdit.Properties.ReadOnly = b
        Me.TenkhachhangTextEdit.Properties.ReadOnly = b
        Me.DiachiTextEdit.Properties.ReadOnly = b
        Me.HinhThucThanhToanSearchLookUpEdit.Properties.ReadOnly = b
        Me.DieuKienGiaoHangTextEdit.Properties.ReadOnly = b
        Me.TenNguoiGiaoDichnTextEdit.Properties.ReadOnly = b
        Me.BoPhanSearchLookUpEdit.Properties.ReadOnly = b
        Me.MaNSXSearchLookUpEdit.Properties.ReadOnly = b
        Me.TinhtrangSearchLookUpEdit.Properties.ReadOnly = b
        Me.NgayThanhToanDateEdit.Properties.ReadOnly = b
        Me.SoThamChieuTextEdit.Properties.ReadOnly = b
        Me.NoiDungTextEdit.Properties.ReadOnly = b
        Me.LoaitienSearchLookUpEdit.Properties.ReadOnly = b
        Me.NguyenteTextEdit.Properties.ReadOnly = b
        Me.ThanhtienvndTextEdit.Properties.ReadOnly = b
        Me.TygianganhangTextEdit.Properties.ReadOnly = b
        Me.ManghiepvuSearchLookUpEdit.Properties.ReadOnly = b
        Me.SohoadonTextEdit.Properties.ReadOnly = b
        Me.NgayhoadonDateEdit.Properties.ReadOnly = b
        Me.KhoanmucphiSearchLookUpEdit.Properties.ReadOnly = b
        Me.MakhoSearchLookUpEdit.Properties.ReadOnly = b
        Me.SohopdongTextEdit.Properties.ReadOnly = b
        Me.MaCTKMSearchLookUpEdit.Properties.ReadOnly = b
        Me.SoSOTextEdit.Properties.ReadOnly = b
        Me.SoPOTextEdit.Properties.ReadOnly = b
        Me.LenhsanxuatSearchLookUpEdit.Properties.ReadOnly = b
        Me.TygiaTKHQTextEdit.Properties.ReadOnly = b
        Me.DiengiaiTextEdit.Properties.ReadOnly = b
        Me.NgaytokhaihaiquanDateEdit.Properties.ReadOnly = b
        Me.SogiayphepnhapkhauTextEdit.Properties.ReadOnly = b
        Me.SoThamChieuTextEdit.Properties.ReadOnly = b
        Me.SothamchieuTextEdit1.Properties.ReadOnly = b
        Me.SotokhaihaiquanTextEdit.Properties.ReadOnly = b
        Me.TennguoigiaodichTextEdit.Properties.ReadOnly = b
        If b Then
            Me.IGridViewHangHoa.OptionsBehavior.Editable = False
            Me.IGridViewThue.OptionsBehavior.Editable = False
            Me.IGridViewChatLuong.OptionsBehavior.Editable = False
        Else
            Me.IGridViewHangHoa.OptionsBehavior.Editable = True
            Me.IGridViewThue.OptionsBehavior.Editable = True
            Me.IGridViewChatLuong.OptionsBehavior.Editable = True
        End If
        If b Then 'trong trạng thái readonly  thì không cho nhập và dòng trống thêm ở cuối
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else  'cho thêm dòng trống ở cuối (khác form đơn - bỏ dòng : And State = 1)
            Me.IGridViewHangHoa.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
            Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.KHHangHoaTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoa)
        'Me.KHHangHoaChiTietTableAdapter.Fill(Me.PCV_FISHDataSet.KHHangHoaChiTiet)
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        Me.DmnghiepvuTableAdapter.Fill(Me.PCV_FISHDataSet.dmnghiepvu)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.DmnguonvonTableAdapter.Fill(Me.PCV_FISHDataSet.dmnguonvon)
        Me.DmkheuocvayTableAdapter.Fill(Me.PCV_FISHDataSet.dmkheuocvay)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhoanmucphi)
        Me.DmnganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmnganhang)
        Me.DmhanghoaTableAdapter.FillByMaTenDonViTinh(Me.PCV_FISHDataSet.dmhanghoa)
        Me.KHThueTableAdapter.Fill(Me.PCV_FISHDataSet.KHThue)
        Me.DmkhoTableAdapter.Fill(Me.PCV_FISHDataSet.dmkho)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        Me.GetDMNghiepVu_MuaHangTrongNuocTableAdapter.Fill(Me.PCV_FISHDataSet.getDMNghiepVu_MuaHangTrongNuoc)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.KHQuanLyViTriHangTableAdapter.Fill(Me.PCV_FISHDataSet.KHQuanLyViTriHang)
        setReadonly(True)
        Me.TC_TienIGridControl.ForceInitialize()
        Me.KHHangHoaBindingSource.MoveLast()
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.loadDuLieu()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonly(False)
        'Mã //set ngầm
        Me.ManghiepvuSearchLookUpEdit.Focus()
        Me.ManghiepvuSearchLookUpEdit.ShowPopup()
        Dim row As DataRowView = CType(KHHangHoaBindingSource.AddNew(), DataRowView)
        Me.Ma = "MHNN" & System.Guid.NewGuid.ToString()
        row("ma") = Ma
        Me.LoaitienSearchLookUpEdit.EditValue = "VND"
        Me.ThanhtienvndTextEdit.EditValue = "0"
        Me.NguyenteTextEdit.EditValue = "0"
        Me.TinhtrangSearchLookUpEdit.EditValue = "NL"
        'mặc định
        Me.NgaychungtuDateEdit.EditValue = Today
        Me.NgayThanhToanDateEdit.EditValue = Today
        Me.TygianganhangTextEdit.EditValue = 1
        Me.State = 1
    End Sub

    Private Sub MakhachhangSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MaDoiTuongSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.MaDoiTuongSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TenkhachhangTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "ten")
        Me.DiachiTextEdit.EditValue = editor.Properties.View.GetRowCellValue(rowhandle, "diachi")
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewThue.PostEditor()
            Me.IGridViewThue.UpdateCurrentRow()
            Me.IGridViewThue.CloseEditor()
            Me.IGridViewHangHoa.PostEditor()
            Me.IGridViewHangHoa.UpdateCurrentRow()
            Me.IGridViewHangHoa.CloseEditor()
            Me.KHHangHoaBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
            Me.FKKHThueKHHangHoaBindingSource.EndEdit()
            'Me.FKTCThueTCThue1BindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonly(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonly(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub NhanviensaleSearchLookUpEdit_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles BoPhanSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.BoPhanSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        'Me.DmphongbanBindingSource.Filter = "ma = '" & editor.Properties.View.GetRowCellValue(rowhandle, "bophan") & "'"
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonly(False)
        Me.State = 2
    End Sub

    Private Sub IGridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewHangHoa.CellValueChanged
        'If e.Column.FieldName = "dongianguyente" Or e.Column.FieldName = "soluonggiao" Or e.Column.FieldName = "dongiavnd" Or e.Column.FieldName = "tilethueGTGT" Then
        '    Try
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtiennguyente"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("soluonggiao"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("dongianguyente"))))
        '    Catch ex As Exception
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtiennguyente"), 0)
        '    End Try
        '    Try
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("soluonggiao"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("dongiavnd"))))
        '    Catch ex As Exception
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"), 0)
        '    End Try
        '    Try
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tienthueGTGT"), CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("thanhtienvnd"))) * CDec(Me.IGridViewTienChiTiet.GetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tilethueGTGT"))) / 100)
        '    Catch ex As Exception
        '        IGridViewTienChiTiet.SetRowCellValue(e.RowHandle, IGridViewTienChiTiet.Columns("tienthueGTGT"), 0)
        '    End Try
        'End If
        'Me.IGridViewTienChiTiet.UpdateSummary()
    End Sub

    Private Sub IGridViewTienChiTiet_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles IGridViewHangHoa.FocusedRowChanged
        Dim currentView As GridView = CType(sender, GridView)
        ' The group row's handle. 
        Dim groupRowHandle As Integer
        If (e.FocusedRowHandle >= 0) Then
            groupRowHandle = currentView.GetParentRowHandle(e.FocusedRowHandle)
        Else
            groupRowHandle = e.FocusedRowHandle
        End If
        If Not currentView.IsGroupRow(groupRowHandle) Then Return

        ' Get group summary values. 
        Dim ht As Hashtable = currentView.GetGroupSummaryValues(groupRowHandle)
        Text = ""
        ' Iterate through group summaries. 
        Dim entry As DictionaryEntry
        For Each entry In ht
            Dim sumItem As GridGroupSummaryItem = entry.Key
            Dim val As Object = entry.Value
            ' Get the summry item's Tag property and summary value. 
            Try
                'Text += sumItem.Tag.ToString() + ": " + val.ToString() + "\t"
                MessageBox.Show(val.ToString)
            Catch ex As Exception

            End Try

        Next
    End Sub
    Private Sub IGridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewHangHoa.InitNewRow
        Try
            'If State = 1 Then
            '    Me.IGridViewHangHoa.SetFocusedRowCellValue("ma", Ma)
            'End If
            Me.IGridViewHangHoa.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("manghiepvu", Me.ManghiepvuSearchLookUpEdit.EditValue)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
            Me.IGridViewHangHoa.SetFocusedRowCellValue("soluongDN", 0) 'phân biệt chữ hoa thường
            Me.IGridViewHangHoa.SetFocusedRowCellValue("soluonggiaodich", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiangoaite", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("dongiavnd", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienngoaite", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienvnd", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueGTGT", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueGTGT", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("chiphimua", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tilethueNK", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("tienthueNK", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("giavonnhap", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtiennhap", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("thanhtienxuat", 0)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanno", TaiKhoanNo)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanco", TaiKhoanCo)
            Me.IGridViewHangHoa.SetFocusedRowCellValue("taikhoanthueGTGT", TaiKhoanThue)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        Me.IGridViewHangHoa.MoveLast()
        Me.IGridViewHangHoa.Focus()
        Me.IGridViewHangHoa.AddNewRow()
        Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
        Me.IGridViewHangHoa.ShowEditor()
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit5_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs)
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.KHHangHoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.KHHangHoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.KHHangHoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.KHHangHoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveFirst()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MovePrevious()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveNext()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            Me.FKKHHangHoaChiTietKHHangHoaBindingSource.MoveLast()
        End If
        If Me.LayoutControlGroupChatLuong.IsInTabbedGroup Then

        End If
    End Sub
    '============= bắt lỗi igridview1 =========================
    Private Sub IGridView1_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles IGridViewHangHoa.ShownEditor
        Dim view As ColumnView = CType(sender, ColumnView)
        view.ActiveEditor.IsModified = True
        If TypeOf Me.IGridViewHangHoa.ActiveEditor Is SearchLookUpEdit And IsDBNull(Me.IGridViewHangHoa.FocusedValue) Then
            DirectCast(IGridViewHangHoa.ActiveEditor, SearchLookUpEdit).ShowPopup()
        End If
    End Sub

    Private Sub IGridView1_ValidatingEditor(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridViewHangHoa.ValidatingEditor
        If Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanno" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
                Exit Sub
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "taikhoanco" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "mahang" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "solo" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "hsd" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "soluongSO" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "soluonggiao" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf CDec(e.Value) = 0 Then
                e.ErrorText = "không được bằng 0 !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "tilethueGTGT" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        ElseIf Me.IGridViewHangHoa.FocusedColumn.FieldName = "tienthueGTGT" And IGridViewHangHoa.FocusedRowHandle <> -999997 Then
            If IsDBNull(e.Value) Or IsNothing(e.Value) Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            ElseIf e.Value.ToString.Trim = "" Then
                e.ErrorText = "không được rỗng !"
                e.Valid = False
            End If
        End If
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.SochungtuTextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.KHHangHoaBindingSource.RemoveCurrent()
            'Me.TCThueTableAdapter.Update(Me.PCV_FISHDataSet.TCThue)
            Me.setReadonly(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.KHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        If Me.LayoutControlGroupHangHoa.IsInTabbedGroup Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewHangHoa.GetRowCellValue(Me.IGridViewHangHoa.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.IGridViewHangHoa.DeleteRow(Me.IGridViewHangHoa.FocusedRowHandle)
            End If
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.FKKHHangHoaChiTietKHHangHoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần 'hàng hóa' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        'load dữ liệu lại nhưng giữ nguyên phiếu
        Dim position As Integer = Me.KHHangHoaBindingSource.Position
        loadDuLieu()
        Me.KHHangHoaBindingSource.Position = position
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKhachHang_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKhachHang.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("madonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("tendonvi", editor.Properties.View.GetRowCellValue(rowHandle, "ten"))
    End Sub

    Private Sub RepositoryItemSearchLookUpEditKheUocVay_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEditKheUocVay.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        Me.IGridViewHangHoa.SetFocusedRowCellValue("kheuocvay", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        Me.IGridViewHangHoa.SetFocusedRowCellValue("hopdongtindung", editor.Properties.View.GetRowCellValue(rowHandle, "sohopdongtindung"))
    End Sub

    Private Sub BarButtonItemChuyenDuLieuThue_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemChuyenDuLieuThue.ItemClick
        If Me.TabbedControlGroup2.SelectedTabPageIndex = 2 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu thuế tự động không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                If Me.IGridViewHangHoa.GetGroupRowValue(Me.IGridViewHangHoa.FocusedRowHandle) Then
                End If
                Try
                    For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                        If Me.IGridViewHangHoa.GetRowCellValue(index, "taikhoanno").ToString = "133" Then
                            TienThue += CDec(Me.IGridViewHangHoa.GetRowCellValue(index, "tienvnd"))
                        End If
                    Next
                Catch ex As Exception
                End Try
                Me.TongCong = CDec(Me.IGridViewHangHoa.Columns("tienvnd").SummaryItem.SummaryValue)
                Try
                    Me.PhanTramThue = TienThue / TongCong * 100
                Catch ex As Exception
                    Me.PhanTramThue = 0
                    MessageBox.Show(ex.Message) 'loại trừ chia cho 0
                End Try
                Me.DoanhThu = TongCong - TienThue
                Me.IGridViewThue.AddNewRow()
                'bỏ group by
            End If
        End If

        If Me.TabbedControlGroup2.SelectedTabPageIndex = 4 Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn chuyển dữ liệu 'quản lý vị trí hàng '  không ?'", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                For index = 0 To Me.IGridViewHangHoa.RowCount - 1
                    Me.MaHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("mahang")
                    Me.TenHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("tenhang")
                    Me.DVT = Me.IGridViewHangHoa.GetFocusedRowCellValue("dvt")
                    Me.LoHang = Me.IGridViewHangHoa.GetFocusedRowCellValue("solo")
                    Me.HSD = CDate(Me.IGridViewHangHoa.GetFocusedRowCellValue("hsd"))
                    Me.IGridViewQuanLyViTriHang.AddNewRow()
                Next
            End If
        End If
    End Sub

    Private Sub IGridViewThue_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewThue.InitNewRow
        'set mặc định
        If State = 1 Then
            Me.IGridViewThue.SetFocusedRowCellValue("ma", Ma)
        End If
        Me.IGridViewThue.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewThue.SetFocusedRowCellValue("manghiepvu", "MHTN")
        Me.IGridViewThue.SetFocusedRowCellValue("sochungtu", Me.SochungtuTextEdit.EditValue)
        Me.IGridViewThue.SetFocusedRowCellValue("ngaychungtu", CDate(Me.NgaychungtuDateEdit.EditValue))
        '==================================================================
        Me.IGridViewThue.SetFocusedRowCellValue("doanhthu", Me.DoanhThu)
        Me.IGridViewThue.SetFocusedRowCellValue("phantramthue", Me.PhanTramThue) 'phân biệt chữ hoa thường
        Me.IGridViewThue.SetFocusedRowCellValue("tienthue", Me.TienThue)
        Me.IGridViewThue.SetFocusedRowCellValue("tongcong", Me.TongCong)
    End Sub

    Private Sub LoaitienSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoaitienSearchLookUpEdit.GotFocus
        If IsDBNull(Me.LoaitienSearchLookUpEdit.EditValue) And State <> 0 And LoaitienSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
        If Me.State <> 0 Then
            'Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, CDate(Me.NgaychungtuDateEdit.EditValue))
            Me.DmtygiaBindingSource.Filter = " ngaytygia = '" & Format(CDate(Me.NgaychungtuDateEdit.EditValue), "yyyy-MM-dd") & "'"
        End If
    End Sub

    Private Sub MakhoSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MakhoSearchLookUpEdit.GotFocus
        If IsDBNull(Me.MakhoSearchLookUpEdit.EditValue) And State <> 0 And MakhoSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub MaNSXSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaNSXSearchLookUpEdit.GotFocus
        If IsDBNull(Me.MaNSXSearchLookUpEdit.EditValue) And State <> 0 And MaNSXSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub MaDoiTuongSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaDoiTuongSearchLookUpEdit.GotFocus
        If IsDBNull(Me.MaDoiTuongSearchLookUpEdit.EditValue) And State <> 0 And MaDoiTuongSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub BoPhanSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles BoPhanSearchLookUpEdit.GotFocus
        If IsDBNull(Me.BoPhanSearchLookUpEdit.EditValue) And State <> 0 And BoPhanSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub KhoanmucphiSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles KhoanmucphiSearchLookUpEdit.GotFocus
        If IsDBNull(Me.KhoanmucphiSearchLookUpEdit.EditValue) And State <> 0 And KhoanmucphiSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub MakhoSearchLookUpEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakhoSearchLookUpEdit.EditValueChanged
        If IsDBNull(Me.MakhoSearchLookUpEdit.EditValue) And State <> 0 And MakhoSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub MaCTKMSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaCTKMSearchLookUpEdit.GotFocus
        If IsDBNull(Me.MaCTKMSearchLookUpEdit.EditValue) And State <> 0 And MaCTKMSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub LenhsanxuatSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles LenhsanxuatSearchLookUpEdit.GotFocus
        If IsDBNull(Me.LenhsanxuatSearchLookUpEdit.EditValue) And State <> 0 And LenhsanxuatSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub


    Private Sub TinhtrangSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TinhtrangSearchLookUpEdit.GotFocus
        If IsDBNull(Me.TinhtrangSearchLookUpEdit.EditValue) And State <> 0 And TinhtrangSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub HinhThucThanhToanSearchLookUpEdit_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles HinhThucThanhToanSearchLookUpEdit.GotFocus
        If IsDBNull(Me.HinhThucThanhToanSearchLookUpEdit.EditValue) And State <> 0 And HinhThucThanhToanSearchLookUpEdit.IsPopupOpen = False Then
            SendKeys.Send("%{DOWN}")
        End If
    End Sub

    Private Sub TinhtrangSearchLookUpEdit_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TinhtrangSearchLookUpEdit.KeyDown
        If State <> 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab) Then
            Me.TabbedControlGroup2.SelectedTabPage = Me.TabbedControlGroup2.TabPages(0) 'chọn tab hàng hóa
            'Me.BarButtonItemTaoMoiChiTiet.PerformClick
            Me.KHHangHoaBindingSource.EndEdit()
            Me.IGridViewHangHoa.MoveLast()
            Me.IGridViewHangHoa.Focus()
            Me.IGridViewHangHoa.AddNewRow()
            Me.IGridViewHangHoa.FocusedColumn = Me.IGridViewHangHoa.VisibleColumns(0)
            Me.IGridViewHangHoa.ShowEditor()
        End If
    End Sub

    Private Sub RepositoryItemSearchLookUpEdit_HangHoa_EditValueChanging(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemSearchLookUpEdit_HangHoa.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        IGridViewHangHoa.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "ma"))
        IGridViewHangHoa.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "ten1"))
        IGridViewHangHoa.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "donvitinh"))
    End Sub

    Private Sub IGridViewQuanLyViTriHang_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewQuanLyViTriHang.InitNewRow
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("uid", System.Guid.NewGuid().ToString)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("mahang", Me.MaHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("tenhang", Me.TenHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("lohang", Me.LoHang)
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("dvt", DVT) 'phân biệt chữ hoa thường
        Me.IGridViewQuanLyViTriHang.SetFocusedRowCellValue("hsd", Me.HSD)
    End Sub

    Private Sub ManghiepvuSearchLookUpEdit_EditValueChanging(sender As System.Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles ManghiepvuSearchLookUpEdit.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowhandle As Integer = Me.ManghiepvuSearchLookUpEdit.Properties.GetIndexByKeyValue(e.NewValue)
        Me.TaiKhoanNo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanno")
        Me.TaiKhoanCo = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanco")
        Me.TaiKhoanThue = editor.Properties.View.GetRowCellValue(rowhandle, "taikhoanvat")
    End Sub
End Class
